<template>
	<view class="shop-car">
		<commonCar :list="goodsProducts" @delect="delect" @accounts="accounts" @deleteOne="deleteOne">
		</commonCar>
	</view>
</template>

<script>
	import commonCar from './shopCar.vue'
	import {
		getShopList,
		deleteOne
	} from '../../jpa/cart';
	export default {
		data() {
			return {
				goodsProducts: [],
			}
		},
		components: {
			commonCar
		},
		onShow() {
			this.loginTest();
			this.shopCartList();
		},
		methods: {
			deleteOne(cartId) {
				deleteOne(cartId).then(data => {
					if (data == 0) {
						this.shopCartList();
					}
				})
			},
			loginTest() {
				console.log("进入该方法")
				let token = uni.getStorageSync("token")
				if (token == "") {
					uni.navigateTo({
						url: "/pages/component/login"
					})
				}
			},
			delect(e) {
				for (var i = 0; i < e.length; i++) {
					deleteOne(e[i]).then(data => {
						if (data == 0) {
							this.shopCartList();
						} else {
							uni.showToast({
								title: "删除失败",
								icon: 'error'
							})
						}
					})
				}
			},
			accounts(e) {
				console.log(e);
			},
			shopCartList() {
				getShopList().then(item => {
					this.goodsProducts = []
					for (var i = 0; i < item.brandList.length; i++) {
						this.goodsProducts.push({
							name: item.brandList[i].name,
							checked: 1,
							goods: []
						})
						for (var j = 0; j < item.shopCartList.length; j++) {
							if (item.shopCartList[j].brandId == item.brandList[i].id) {
								let good = {
									img: item.shopCartList[j].productSku.defaultImg,
									title: item.shopCartList[j].productSpu.productSpuName,
									remark: item.shopCartList[j].productSku.productSkuName,
									price: item.shopCartList[j].productSku.price,
									number: item.shopCartList[j].byNum,
									cartId: item.shopCartList[j].cartId,
									checked:1
								}
								this.goodsProducts[i].goods.push(good);
							}
						}
					}
					console.log(this.goodsProducts)
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.shop-car {
		width: 750rpx;
		min-height: 100vh;
		display: flex;
		flex-direction: column;
		align-items: center;
		background: #F5F5F5;
	}
</style>